<template>
  <div class="action-panel">
    <div class="panel-header">
      <h3>操作控制</h3>
      
      <!-- 模式切换 -->
      <div class="mode-switch">
        <label class="switch">
          <input 
            type="checkbox" 
            :checked="mode === 'auto'"
            @change="handleModeChange"
          >
          <span class="slider"></span>
        </label>
        <span class="mode-label">
          {{ mode === 'auto' ? '自动模式' : '手动模式' }}
        </span>
      </div>
    </div>
    
    <!-- 自动模式控制 -->
    <div class="auto-controls" v-if="mode === 'auto'">
      <div class="control-group">
        <button 
          class="btn btn-primary btn-large"
          :disabled="isRunning"
          @click="handleStartComplete"
        >
          <i class="fas fa-play"></i>
          {{ isRunning ? '执行中...' : '开始完整检测' }}
        </button>
        
        <button 
          class="btn btn-secondary"
          :disabled="!isRunning"
          @click="handleStop"
        >
          <i class="fas fa-stop"></i>
          停止
        </button>
      </div>
      
      <div class="auto-options">
        <label class="checkbox-label">
          <input 
            type="checkbox" 
            v-model="forceUpdate"
            :disabled="isRunning"
          >
          强制更新（忽略缓存）
        </label>
      </div>
    </div>
    
    <!-- 手动模式控制 -->
    <div class="manual-controls" v-if="mode === 'manual'">
      <div class="control-group">
        <button 
          class="btn btn-execute"
          :disabled="!nextExecutableStep"
          @click="handleExecuteNext"
        >
          <i class="fas fa-play"></i>
          {{ nextExecutableStep ? `执行: ${nextExecutableStep.name}` : '无可执行步骤' }}
        </button>
        
        <button 
          class="btn btn-secondary"
          :disabled="!hasErrors"
          @click="handleRetryAll"
        >
          <i class="fas fa-redo"></i>
          重试失败步骤
        </button>
      </div>
      
      <div class="manual-options">
        <button 
          class="btn btn-warning btn-small"
          @click="handleReset"
          :disabled="isRunning"
        >
          <i class="fas fa-refresh"></i>
          重置所有步骤
        </button>
      </div>
    </div>
    
    <!-- 系统信息面板 -->
    <div class="system-info" v-if="systemInfo">
      <h4>系统状态</h4>
      <div class="info-grid">
        <div class="info-item">
          <span class="info-label">运行容器:</span>
          <span class="info-value">{{ systemInfo.container_count || 0 }}</span>
        </div>
        <div class="info-item">
          <span class="info-label">当前学校:</span>
          <span class="info-value">{{ systemInfo.current_school?.school_name || '未设置' }}</span>
        </div>
        <div class="info-item">
          <span class="info-label">WebSocket:</span>
          <span class="info-value" :class="wsStatusClass">
            {{ wsConnected ? '已连接' : '已断开' }}
          </span>
        </div>
        <div class="info-item">
          <span class="info-label">系统状态:</span>
          <span class="info-value" :class="systemStatusClass">
            {{ systemInfo.system_status || 'unknown' }}
          </span>
        </div>
      </div>
    </div>
    
    <!-- 快速操作 -->
    <div class="quick-actions">
      <h4>快速操作</h4>
      <div class="action-buttons">
        <button 
          class="btn btn-info btn-small"
          @click="handleRefreshSystem"
        >
          <i class="fas fa-sync"></i>
          刷新系统状态
        </button>
        
        <button 
          class="btn btn-secondary btn-small"
          @click="handleExportLogs"
        >
          <i class="fas fa-file-export"></i>
          导出状态
        </button>
        
        <button 
          class="btn btn-warning btn-small"
          @click="handleReconnectWS"
          v-if="!wsConnected"
        >
          <i class="fas fa-wifi"></i>
          重连WebSocket
        </button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ActionPanel',
  props: {
    mode: {
      type: String,
      default: 'manual'
    },
    isRunning: {
      type: Boolean,
      default: false
    },
    nextExecutableStep: {
      type: Object,
      default: null
    },
    hasErrors: {
      type: Boolean,
      default: false
    },
    systemInfo: {
      type: Object,
      default: null
    },
    wsConnected: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      forceUpdate: false
    }
  },
  computed: {
    wsStatusClass() {
      return this.wsConnected ? 'status-online' : 'status-offline'
    },
    systemStatusClass() {
      const status = this.systemInfo?.system_status
      switch (status) {
        case 'running':
          return 'status-online'
        case 'idle':
          return 'status-idle'
        default:
          return 'status-unknown'
      }
    }
  },
  methods: {
    handleModeChange(event) {
      const newMode = event.target.checked ? 'auto' : 'manual'
      this.$emit('mode-change', newMode)
    },
    
    handleStartComplete() {
      this.$emit('start-complete', { forceUpdate: this.forceUpdate })
    },
    
    handleStop() {
      this.$emit('stop')
    },
    
    handleExecuteNext() {
      if (this.nextExecutableStep) {
        this.$emit('execute-step', this.nextExecutableStep.type)
      }
    },
    
    handleRetryAll() {
      this.$emit('retry-all')
    },
    
    handleReset() {
      this.$emit('reset')
    },
    
    handleRefreshSystem() {
      this.$emit('refresh-system')
    },
    
    handleExportLogs() {
      this.$emit('export-logs')
    },
    
    handleReconnectWS() {
      this.$emit('reconnect-ws')
    }
  }
}
</script>

<style scoped>
.action-panel {
  background: white;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.panel-header h3 {
  margin: 0;
  font-size: 16px;
  color: #374151;
}

/* 模式切换开关 */
.mode-switch {
  display: flex;
  align-items: center;
  gap: 8px;
}

.switch {
  position: relative;
  display: inline-block;
  width: 48px;
  height: 24px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  transition: 0.4s;
  border-radius: 24px;
}

.slider:before {
  position: absolute;
  content: "";
  height: 18px;
  width: 18px;
  left: 3px;
  bottom: 3px;
  background-color: white;
  transition: 0.4s;
  border-radius: 50%;
}

input:checked + .slider {
  background-color: #3b82f6;
}

input:checked + .slider:before {
  transform: translateX(24px);
}

.mode-label {
  font-size: 14px;
  font-weight: 500;
  color: #374151;
}

/* 控制组 */
.control-group {
  display: flex;
  gap: 12px;
  align-items: center;
}

.auto-options,
.manual-options {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
}

/* 按钮样式 */
.btn {
  padding: 8px 16px;
  border: 1px solid transparent;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.2s ease;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-large {
  padding: 12px 24px;
  font-size: 16px;
}

.btn-small {
  padding: 6px 12px;
  font-size: 12px;
}

.btn-primary {
  background: #3b82f6;
  color: white;
  border-color: #3b82f6;
}

.btn-primary:hover:not(:disabled) {
  background: #2563eb;
}

.btn-secondary {
  background: #6b7280;
  color: white;
  border-color: #6b7280;
}

.btn-secondary:hover:not(:disabled) {
  background: #4b5563;
}

.btn-execute {
  background: #10b981;
  color: white;
  border-color: #10b981;
}

.btn-execute:hover:not(:disabled) {
  background: #059669;
}

.btn-warning {
  background: #f59e0b;
  color: white;
  border-color: #f59e0b;
}

.btn-warning:hover:not(:disabled) {
  background: #d97706;
}

.btn-info {
  background: #0ea5e9;
  color: white;
  border-color: #0ea5e9;
}

.btn-info:hover:not(:disabled) {
  background: #0284c7;
}

/* 复选框 */
.checkbox-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  color: #374151;
  cursor: pointer;
}

.checkbox-label input[type="checkbox"] {
  width: 16px;
  height: 16px;
}

/* 系统信息 */
.system-info h4,
.quick-actions h4 {
  margin: 0 0 8px 0;
  font-size: 14px;
  color: #374151;
  border-bottom: 1px solid #e5e7eb;
  padding-bottom: 4px;
}

.info-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.info-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4px 0;
}

.info-label {
  font-size: 12px;
  color: #6b7280;
}

.info-value {
  font-size: 12px;
  font-weight: 500;
  color: #374151;
}

.status-online {
  color: #10b981;
}

.status-offline {
  color: #ef4444;
}

.status-idle {
  color: #f59e0b;
}

.status-unknown {
  color: #6b7280;
}

/* 快速操作 */
.action-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

/* 响应式 */
@media (max-width: 640px) {
  .control-group {
    flex-direction: column;
    align-items: stretch;
  }
  
  .info-grid {
    grid-template-columns: 1fr;
  }
  
  .action-buttons {
    flex-direction: column;
  }
}
</style>